package com.example.demo.service.impl;

import com.example.demo.entity.Soft;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.If;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.example.demo.mapper.SoftMapper;
import com.example.demo.service.SoftService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.demo.entity.table.SoftTableDef.SOFT;

/**
 *  服务层实现。
 *
 * @author zql
 * @since 2023-11-05
 */
@Service
@Slf4j
public class SoftServiceImpl extends ServiceImpl<SoftMapper, Soft> implements SoftService {
    @Override
    public Page<Soft> queryFuzzy(Integer current, Integer size, String softId,
                                       String softName, String softVersion,
                                       String gitUrl, String officialUrl, String mainLanguage,
                                       String languages, String startTime, String endTime) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(SOFT.DEFAULT_COLUMNS).from(SOFT)
                .where(SOFT.SOFT_NAME.like(softName, If::hasText))
                .and(SOFT.SOFT_VERSION.like(softVersion, If::hasText))
                .and(SOFT.GIT_URL.like(gitUrl, If::hasText))
                .and(SOFT.OFFICIAL_URL.like(officialUrl, If::hasText))
                .and(SOFT.MAIN_LANGUAGE.like(mainLanguage, StringUtil::isNotBlank))
                .and(SOFT.LANGUAGES.like(languages, If::hasText))
                .and(SOFT.SOFT_ID.like(softId, If::hasText))
                .and(SOFT.UPDATED_AT.between(startTime + " 00:00:00", endTime + " 23:59:59").when(!"".equals(startTime) &&  !"".equals(endTime)))
                .orderBy(SOFT.UPDATED_AT, false); // asc=false 就是desc!
                                //.orderByDesc(SOFT.UPDATED_AT); // mybatis-flex 时间类型不支持orderByDesc??
        log.info("queryWrapper::: " + queryWrapper.toSQL());
        Page<Soft> record = page(new Page<>(
                current,
                size
        ), queryWrapper);
        log.info("record::: " + record);
        return record;
    }

    @Override
    public Page<Soft> pageDef(Integer current, Integer size, String softName) {
        // mybatis-plus // LambdaQueryWrapper<Soft> queryWrapper = new LambdaQueryWrapper<>();
        // mybatis-plex 没有LambdaQueryWrapper， 也不能QueryWrapper<Soft> 这么用！

        // mybatis-flex
         QueryWrapper queryWrapper = new QueryWrapper();

        if (!"".equals(softName)) {
            // 精确查询
            // queryWrapper.eq(Soft::getSoftName(softName)); // plus
            /*queryWrapper.select(SOFT.ALL_COLUMNS)
                    .where(SOFT.SOFT_NAME.eq(softName));*/

            // 模糊查询
            // queryWrapper.like(Soft::getSoftName(softName)); // plus
            queryWrapper.select(SOFT.ALL_COLUMNS)
                    // .orderBy(SOFT.ID) // 报错的 // flex 没有DESC??
                    .where(SOFT.SOFT_NAME.like(softName));
        }

        /*QueryWrapper queryWrapper1 = QueryWrapper.create()
                .select(SOFT.ALL_COLUMNS)
                .where(SOFT.SOFT_NAME.eq(softName));*/

        Page<Soft> page = page(new Page<>(
            current,
            size
        ), queryWrapper);

        return page;
    }

    @Override
    public boolean saveOrUpdateByIdsDef(Soft soft) {
        if (soft.getId() != null) {
            return updateById(soft); // IService 自带的!
        } else {
            return save(soft); // IService 自带的!
        }
    }

    @Override
    public boolean deleteBatchIdsDef(List<Integer> ids) {
        // return deleteBatchIds(ids); // plus
        // removeBatchIds flex没有这个方法
        return removeByIds(ids); // IService 自带的!
    }
}
